<template>
  <div class="register">
    <el-form
      class="register-form"
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
    >
      <div class="title">注册页</div>
      <el-form-item prop="phone">
        <el-input
          v-model="ruleForm.phone"
          type="text"
          placeholder="请输入手机号"
          size="large"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="pass">
        <el-input
          v-model="ruleForm.pass"
          type="password"
          placeholder="请输入密码"
          size="large"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="name">
        <el-input
          v-model="ruleForm.name"
          type="text"
          placeholder="请输入姓名"
          size="large"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="role">
        <el-select
          v-model="ruleForm.role"
          placeholder="请选择角色"
          size="large"
        >
          <el-option
            v-for="item in rolelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >注册</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { role_list, user_register } from "../api/index";

let ruleForm = ref({ phone: "", pass: "", name: "", role: "" });
let ruleFormRef = ref();
let rolelist = ref([]);
let $router = useRouter();

onMounted(async () => {
  // 请求角色列表
  let res = await role_list();
  let arr = res.data.list.map((item) => {
    return { label: item.name, value: item._id };
  });
  rolelist.value = arr;
});

const validatePhone = (rule, value, callback) => {
  if (/^1[3-9]\d{9}$/.test(value) == false) {
    callback(new Error("请输入正确的手机号"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (/^\d{4}$/.test(value) == false) {
    callback(new Error("请输入4位密码"));
  } else {
    callback();
  }
};
const validateName = (rule, value, callback) => {
  if (value == "") {
    callback(new Error("姓名不能为空"));
  } else {
    callback();
  }
};
const validateRole = (rule, value, callback) => {
  if (value == "") {
    callback(new Error("请选择一个角色"));
  } else {
    callback();
  }
};
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: "blur" }],
  pass: [{ validator: validatePass, trigger: "blur" }],
  name: [{ validator: validateName, trigger: "blur" }],
  role: [{ validator: validateRole, trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      let res = await user_register(ruleForm.value);     
      if (res.data.code == 200) {
        $router.push("/login");
      } else {
        ElNotification({
          title: "注册通知",
          message: res.data.msg,
          type: "error",
        });
      }
    } else {
      return false;
    }
  });
};
</script>

<style scoped lang="scss">
.register {
  height: 100%;
  background: url("../assets/login.png") no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .register-form {
    padding: 30px;
    min-width: 500px;
    border-radius: 5px;
    background-color: #fff;
  }
  .title {
    margin-bottom: 30px;
    font-size: 26px;
    font-weight: 700;
    text-align: center;
  }
}
</style>